package com.kalvin.kvf.modules.ks.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kalvin.kvf.common.dto.R;
import com.kalvin.kvf.common.utils.ShiroKit;
import com.kalvin.kvf.modules.dy.entity.LoginUser;
import com.kalvin.kvf.modules.hxy.entity.Account;
import com.kalvin.kvf.modules.hxy.entity.Cookies;
import com.kalvin.kvf.modules.hxy.mapper.AccountMapper;
import com.kalvin.kvf.modules.hxy.mapper.CookiesMapper;
import com.kalvin.kvf.modules.ks.entity.KSCookie;
import com.kalvin.kvf.modules.ks.entity.KSIntentionCust;
import com.kalvin.kvf.modules.ks.entity.KSVideo;
import com.kalvin.kvf.modules.ks.mapper.KSCookieMapper;
import com.kalvin.kvf.modules.ks.mapper.KSIntentionCustMapper;
import com.kalvin.kvf.modules.ks.mapper.KSVideoMapper;
import com.kalvin.kvf.modules.sys.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.kalvin.kvf.modules.ks.entity.KSTask;
import com.kalvin.kvf.modules.ks.mapper.KSTaskMapper;
import org.springframework.web.client.RestTemplate;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * <p>
 * 抖音任务表 服务实现类
 * </p>
 *
 * @since 2023-11-13 16:18:02
 */
@Service
public class KSTaskServiceImpl extends ServiceImpl<KSTaskMapper, KSTask> implements KSTaskService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private KSTaskMapper ksTaskMapper;
    @Autowired
    private KSVideoMapper ksVideoMapper;
    @Autowired
    private KSCookieMapper ksCookieMapper;
    @Autowired
    private KSIntentionCustMapper ksIntentionCustMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private CookiesMapper cookiesMapper;

//    @Bean
//    public RestTemplate restTemplate(RestTemplateBuilder builder) {
//        return builder.build();
//    }

    // 添加隧道代理
    @Bean
    public RestTemplate proxy() {
        RestTemplate restTemplate = new RestTemplate();
        SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        simpleClientHttpRequestFactory.setProxy(new Proxy(Proxy.Type.HTTP,new InetSocketAddress("t0.juliangip.com",20784))); // t0.juliangip.com:20784
        restTemplate.setRequestFactory(simpleClientHttpRequestFactory);
        return restTemplate;
    }

    @Override
    public Page<KSTask> listTaskPage(KSTask task) {
        User user = ShiroKit.getUser();
        if(!user.getUsername().equals("admin")){
            task.setPhone(user.getPhone());
        }
        Page<KSTask> page = new Page<>(task.getCurrent(), task.getSize());
        List<KSTask> tasks = baseMapper.selectTaskList(task, page);
        return page.setRecords(tasks);
    }

}
